var https = require('https');
var url=require('url');
var queryString=require('querystring');
var errorCode = require('../config/errorCode');

module.exports = function(app){
    app.get('/post', function(req, res, next) {

            var regUrl = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_toke";
            var post_option = url.parse(regUrl);
            var post_data = queryString.stringify({
                'flow' : 'main',
                'from' : '163mail_right',
                'mobile' : ''
            });
            post_option.method = 'POST';
            post_option.headers = {
                //'Content-Type' : 'application/x-server-form-urlencoded',
                'Content-Type' : 'text/plain',
                'Content-Length' : post_data.length
            };

            var request=https.request(post_option,function(response){
                consol.log(e);
                var responseData='';
                response.on('data',function(chunk){
                    responseData=chunk;
                });

                response.on('end',function(){
                    if(responseData!=''){
                        res.send("post请求结束了"+ responseData.toString());
                    }else{
                        var err={
                            state:response.statusCode,
                            mes:response.status
                        };
                        throw new Error('youcuowu')
                    }

                });
            });
            //请求不成功或者超时之类的错误才会走这个error
            request.on('error', function(e) {
                console.log(e);
                console.log('problem with request: ' + e.message);
                res.statusCode = 666;
                res.json({sucess:false, message:errorCode ,err:e.message});
               // throw e;
                //res.statusCode = 404;
                //res.json({sucess:false, message: '请求失败，超时，请检查',err:''});
            });
            request.write(post_data);
            request.end();
        });
};



